<?php
class Default_Model_MFunction
{
    public function  __construct() {
        ;
    }

    // sap xep mang theo truong filename
    // sap xep nhu string
    // mang duoc sap xep la mang document
    // $type=1 sap xep tang
    // $type=-1 sap xep giam
    public function insertSort(&$array,$filename,$type=-1)
    {
        for($i=1;$i<count($array);$i++)
        {
            $temp=$array[$i];
            $j=$i;
            if($type==1)
            {
                while($j>0 && strtolower($array[$j-1][$filename])> strtolower($temp[$filename]))
                {
                    $array[$j]=$array[$j-1];
                    $j--;
                }
            }
            else if($type==-1)
            {
                while($j>0 && strtolower($array[$j-1][$filename])< strtolower($temp[$filename]))
                {
                    $array[$j]=$array[$j-1];
                    $j--;
                }
            }
            $array[$j]=$temp;
        }

        return $array;
    }

    // $date kieu zend_date
    // 
    public function dateToString($date)
    {
        $arrDate=$date->toArray();
        $year=(int)$arrDate["year"];
        $month = (int)$arrDate["month"];
        if($month<10)
        {
            $month='0'.$month;
        }
        $day = (int)$arrDate["day"];
        if($day<10)
        {
            $day='0'.$day;
        }
        $hour = (int)$arrDate["hour"];
        if($hour<10)
        {
            $hour='0'.$hour;
        }
        $minute = (int)$arrDate["minute"];
        if($minute<10)
        {
            $minute='0'.$minute;
        }
        $second = (int)$arrDate["second"];
        if($second<10)
        {
            $second='0'.$second;
        }

        return ($year.':'.$month.':'.$day.' '.$hour.':'.$minute.':'.$second);
    }

    // chuyen string cua date ve mang gom ngay, thang, nam, gio, phut giay
    // string dau vao dang y:m:d h:m:s
    public function stringDateToArray($str)
    {
        $pos=strpos($str, " ");
        $str1=substr($str, 0,$pos);
        $str2=substr($str, $pos+1);
        $arraydate=explode(":", $str1);
        $arrayHour=explode(":",$str2);
        return array("day"=>$arraydate[2],"month"=>$arraydate[1],"year"=>$arraydate[0],
                     "hour"=>$arrayHour[0],"minute"=>$arrayHour[1],"second"=>$arrayHour[2]);
    }

    // chuyển từ chuỗi ngày tháng dạng d/m/y h:m:s sang y:m:d h:m:s
    // phục vụ cho sắp xếp theo thời gian
    public function standardStringDate($strDate)
    {
        $pos=strpos($strDate, " ");
        $str1=substr($strDate, 0,$pos);
        $str2=substr($strDate, $pos+1);
        $arraydate=explode("-", $str1);
        $arrayHour=explode(":",$str2);
        return $arraydate[2].':'.$arraydate[1].':'.$arraydate[0].' '.$arrayHour[0].':'.$arrayHour[1].':'.$arrayHour[2];
    }

    public function standardstring($str)
    {
        $i=0;
        while($i< strlen($str))
        {
            if($str[$i]==' ' && ($i==0 || $i==strlen($str)-1 || $str[$i+1]==' '))
            {
                if($i>0 && $i<strlen($str)-1)
                {
                    $str=substr($str, 0,$i).  substr($str, $i+1);
                }
                else if($i==0)
                    $str=substr($str, 1);
                else if($i==strlen($str)-1)
                    $str=substr($str, 0,strlen($str)-1);
            }
            else
                $i++;
        }

        return $str;
    }
}
